﻿@{
    ViewBag.Title = "监测标准";
}

<div class="row col-md-12 ml-0 vh-3">
    <strong class="font-rfs">监测标准</strong>
</div>
<div class="row col-md-12 ml-0 min-vh-90">
    <div class="row col-md-12 min-vh-90" style="padding-right:0">
        <div class="row col-md-12 vh-5">
            <select id="Monitoring-Select-Type" class="form-control font-rfs custom-headtoolbar-select"></select>
            <button id="addMonitoring" class="btn btn-rfs"><i class="bi-plus-square"></i> 添加</button>
            <button id="editMonitoring" class="btn btn-rfs"><i class="bi-pencil-square"></i> 编辑</button>
            <button id="delMonitoring" class="btn btn-rfs"><i class="bi-trash"></i> 删除</button>
        </div>
        <div class="table-responsive-sm shadow p-0 bg-white rounded col-md-12 min-vh-84 pre-scrollable">
            <table id="tabMonitoring" class="table table-striped table-bordered table-sm table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>名称</th>
                        <th>类型</th>
                        <th>描述</th>
                        <th>温度高限</th>
                        <th>温度低限</th>
                        <th>湿度高限</th>
                        <th>湿度低限</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </div>
</div>
<div class="modal" id="Monitoring-addMonitoring-myModal" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p id="modalTitle" class="pt-3 pl-3 font-rfs"></p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
                <label id="Monitoring-addMonitoring-ModalID" style="display:none">Monitoring-addMonitoring-myModal</label>
            </div>
            <div class="modal-body">
                <div id="Monitoring-addMonitoring-Embed"></div>
            </div>
        </div>
    </div>
</div>
<div class="modal" id="DeleteConfirm" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p class="pt-3 pl-3 font-rfs">删除</p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <p class="font-rfs">确认删除当前选中记录吗？</p>
            </div>
            <div class="modal-Delete-footer">
                <button id="modal-delete" class="btn btn-outline-primary border-radius-noradius noLeftBorder noRightBorder font-rfs" style="width:50%; height:fit-content">删除</button>
                <button id="modal-cancel" data-dismiss="modal" class="btn btn-outline-primary border-radius-noradius noRightBorder font-rfs" style="width:50%; height:fit-content">取消</button>
            </div>
        </div>
    </div>
</div>

@section Scripts{
    <script>
        var MonitoringId = "";
        $(document).ready(function () {
            LoadType();
        });

        function LoadType() {
            $.post("GetMonitoringType", {
            }, function (data) {
                for (var i = 0; i < data.length; i++) {
                    $("#Monitoring-Select-Type").append(new Option(data[i].type));
                }
                $("#Monitoring-Select-Type").prepend(new Option("", ""));
                $("#Monitoring-Select-Type").val("");
            });
        }

        $("#Monitoring-Select-Type").change(function () {
            LoadMonitoring();
        });

        function LoadMonitoring() {
            var columns = [{ field: "id", visible: false }, { field: "name" }, { field: "type" }, { field: "description" }, { field: "highTemperature" },
            { field: "lowTemperature" }, { field: "highHumidity" }, { field: "lowHumidity" }];

            MonitoringId = "";  /*清空该值*/
            $('#tabMonitoring').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
            $('#tabMonitoring').bootstrapTable({
                method: 'post',
                url: "GetMonitoringListByType",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        type: $("#Monitoring-Select-Type").find("option:selected").text(),
                        limit: params.limit, // 每页显示多少条记录
                        offset: params.offset // 偏移量
                    };
                    return data;
                },
                queryParamsType: "limit", // 服务器端分页，这个参数必需是“limit”
                dataField: "rows",

                uniqueId: "id",          // 设置主键
                sidePagination: "server", // 服务器端分页，这个参数必需设置
                pagination: true,  // 显示分页条,设为true，启用了分页功能
                pageNumber: 1,
                pageSize: 20,   // 默认页大小
                pageList: [10, 20, 30, 50],  // 控制每页数目
                locale: "zh-CN",
                columns: columns,

                selectItemName: "selectItem", // 指定选择行ID
                clickToSelect: true, // 相应单击，自动选中此行
                checkboxHeader: true, // 标题行显示全选按钮

                onClickRow: function (row, $element, field) {
                    $("#tabMonitoring").find("tbody").find("tr").each(function () {
                        $(this).removeClass('changeblueColor');
                    });
                    $element.addClass("changeblueColor");
                    MonitoringId = row.id;
                },
                silent: true,
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#tabMonitoring').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {
                    SetBootstrapStyle();
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        }

        $("#delMonitoring").click(function () {
            if (MonitoringId == "") {
                return false;
            }

            $("#DeleteConfirm").modal({
                backdrop: 'static',
                keyboard: false
            });
        });

        $("#modal-delete").click(function () {
            $.post("DelMonitoring", {
                MonitoringId: MonitoringId
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    $('#tabMonitoring').bootstrapTable('removeByUniqueId', MonitoringId);  // 删除指定的行
                    MonitoringId = "";  // 删除行后，将这个值置空，否则直接编辑信息就会报错。
                } else {
                    alert(data);
                }
            });

            $("#DeleteConfirm").modal('hide');
        });

        $("#addMonitoring").click(function () {
            $("#modalTitle").text("添加监测标准");

            $("#Monitoring-addMonitoring-myModal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $.post("AddMonitoring", {
                ModalID: "Monitoring-addMonitoring-myModal"
            }, function (datas) {
                $("#Monitoring-addMonitoring-Embed").html(datas);
            });
        });

        $("#editMonitoring").click(function () {
            if (MonitoringId.length == 0) {
                alert("请先选择需要编辑的监测标准！");
                return;
            }

            $("#modalTitle").text("编辑监测标准");

            $("#Monitoring-addMonitoring-myModal").modal({
                backdrop: 'static',
                keyboard: false
            });

            $.post("AddMonitoring", {
                MonitoringId: MonitoringId,
                ModalID: "Monitoring-addMonitoring-myModal"
            }, function (datas) {
                $("#Monitoring-addMonitoring-Embed").html(datas);
            });
        });

        $('#Monitoring-addMonitoring-myModal').on('hidden.bs.modal', function (event) {
            if (event.target == this) {
                if (MonitoringId.length > 0) {
                    $.post("GetMonitoringById", {
                        Id: MonitoringId
                    }, function (data) {
                        var row = $('#tabMonitoring').bootstrapTable('getRowByUniqueId', MonitoringId);
                        row.name = data.name;
                        row.type = data.type;
                        row.highTemperature = data.highTemperature;
                        row.lowTemperature = data.lowTemperature;
                        row.highHumidity = data.highHumidity;
                        row.lowHumidity = data.lowHumidity;
                        row.description = data.description;
                        $('#tabMonitoring').bootstrapTable('updateByUniqueId', row);
                    });
                }
            }
        });

    </script>
}
